package com.m1.tzolkin.model.util.zone;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.m1.tzolkin.model.exceptions.StepMoveException;

public class StepsZonesManager<Z extends ISteppable, P> implements IStepsZonesManager<Z, P> {
	
	// ATTRIBUTS
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4352453412642379249L;
	private final Map<Z, IStepsZone<P>> steps;
	
	// CONSTRUCTEUR
	
	public StepsZonesManager(Z[] zones) {
		if (zones == null) {
			throw new IllegalArgumentException();
		}
		steps = new HashMap<Z, IStepsZone<P>>();
		for (Z z : zones) {
			steps.put(z, new StepsZone<P>(z.getHeight()));
		}
	}
	
	// REQUETES

	@Override
	public Collection<Z> getZones() {
		return steps.keySet();
	}

	@Override
	public int getPosition(Z z, P p) {
		return steps.get(z).getStep(p);
	}
	
	@Override
	public boolean canUp(Z z, P p) {
		return steps.get(z).getStep(p) < z.getHeight() - 1;
	}
	
	@Override
	public boolean canDown(Z z, P p) {
		return steps.get(z).getStep(p) > 0;
	}
	
	// COMMANDES

	@Override
	public void addKey(P p) {
		for (Z z : steps.keySet()) {
			steps.get(z).addKey(p);
		}
	}

	@Override
	public void addKey(P p, int i) {
		for (Z z : steps.keySet()) {
			steps.get(z).addKey(p, i);
		}
	}

	@Override
	public void up(Z z, P p) throws StepMoveException {
		steps.get(z).up(p);
	}

	@Override
	public void down(Z z, P p) throws StepMoveException {
		steps.get(z).down(p);
	}
	
	// OUTILS
	
	protected IStepsZone<P> getStepsZone(Z z) {
		return steps.get(z);
	}

}
